package org.stvd.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.stvd.common.Consts;
import org.stvd.common.security.SecurityUserHolder;
import org.stvd.entities.admin.Menus;
import org.stvd.entities.admin.TreeNode;
import org.stvd.service.admin.MenusService;
import org.stvd.service.openim.OpenimUserService;

/**
 * 系统首页请求
 * @author houzx
 */
@Controller
public class IndexController {

    
    @Autowired
    private MenusService menusService; //系统菜单Service
    @Autowired
    private OpenimUserService openimUserService; //即时通讯用户相关service
    
    /**
     * @Description: 首页请求
     * @param request
     * @param map
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String pageLoad(HttpServletRequest request,ModelMap map) {
        List<TreeNode> menuList = new ArrayList<TreeNode>(); // 系统菜单列表
        String userId = SecurityUserHolder.getCurrentUserId(); //登录用户ID
        // 获取顶部菜单列表
        List<Menus> topMenuList = menusService.findByParentID(Consts.GUID, "-1", userId);
        if(topMenuList!=null && topMenuList.size() > 0){
            map.put("topMenuList", topMenuList);
            menuList = menusService.getMenuTreeList(Consts.GUID, userId);
            for (TreeNode node : menuList) {
                if(node.getChildren().size() > 0){
                    node.setActive(true);
                    break;
                }
            }
        }
        map.put("jsonMenu", getMenuHtml(menuList));
        // openimUserService.initOpenimUser();
        return "/layout/index.html";
    }
    
    /**
     * @Description: 首页面板
     * @param request
     * @param map
     * @return
     */
    @RequestMapping(value = "/panel", method = RequestMethod.GET)
    public String onIndePanel(HttpServletRequest request, ModelMap map) {
        return "/layout/panel.html";
    }
    

    /**
     * 格式化菜单
     * @param list
     * @return
     */
    private String getMenuHtml(List<TreeNode> list) {
        String jsonMenu = "[";
        for (TreeNode node : list) {
            String tmp = "", spread = "false", 
                    title = node.getText(), 
                    icon = node.getIconCls(), 
                    children = "", 
                    href = node.getAttributes().get(node.getId()+"url");
            
            spread = "\"spread\":"+"false";
            if (node.getActive()) {
                if (node.getChildren().size() > 0) {
                    spread = "\"spread\":"+"true";
                }
            }
            if (node.getChildren().size() > 0) {
                children = ",\"children\":"+getMenuHtml(node.getChildren());
            }
            
            if(href.indexOf("/") == 0){
                href = href.substring(1);
            }
            tmp = "{"
                    + "\"title\":"+"\""+title+"\","
                    + "\"icon\":"+"\""+icon+"\","
                    + "\"href\":"+"\""+href+"\","
                    + spread + children
                    + "},";
            
            jsonMenu += tmp;
        }
        jsonMenu += "]";
        return jsonMenu;
    }
}
